import Vue from 'vue'
import VueRouter from 'vue-router'
// import Login from '../components/Login.vue'
const Login = () => import(/* webpackChunkName: "Login-Home-Welcome" */ '../components/Login.vue')
// import Home from '../components/Home.vue'
const Home = () => import(/* webpackChunkName: "Login-Home-Welcome" */ '../components/Home.vue')
// import Welcome from '../components/Welcome.vue'
const Welcome = () => import(/* webpackChunkName: "Login-Home-Welcome" */ '../components/Welcome.vue')
// import Users from '../components/users/Users.vue'
const Users = () =>
  import(
    /* webpackChunkName: "Users-Rights-Roles" */ '../components/users/Users.vue'
  )
// import Rights from '../components/power/Rights.vue'
const Rights = () =>
  import(
    /* webpackChunkName: "Users-Rights-Roles" */ '../components/power/Rights.vue'
  )
// import Roles from '../components/power/Roles.vue'
const Roles = () =>
  import(
    /* webpackChunkName: "Users-Rights-Roles" */ '../components/power/Roles.vue'
  )
// import Cate from '../components/goods/goods_cate.vue'
const Cate = () =>
  import(
    /* webpackChunkName: "Cate-Params" */ '../components/goods/goods_cate.vue'
  )
// import Params from '../components/goods/goods_params.vue'
const Params = () =>
  import(
    /* webpackChunkName: "Cate-Params" */ '../components/goods/goods_params.vue'
  )
// import Goods from '../components/goods/goods.vue'
const Goods = () =>
  import(
    /* webpackChunkName: "Goods-AddGoods" */ '../components/goods/goods.vue'
  )
// import AddGoods from '../components/goods/addGoods.vue'
const AddGoods = () =>
  import(
    /* webpackChunkName: "Goods-AddGoods" */ '../components/goods/addGoods.vue'
  )
// import Order from '../components/orders/Orders.vue'
const Order = () =>
  import(
    /* webpackChunkName: "Order-Report" */ '../components/orders/Orders.vue'
  )
// import Report from '../components/report/Report.vue'
const Report = () =>
  import(
    /* webpackChunkName: "Order-Report" */ '../components/report/Report.vue'
  )
Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    children: [
      { path: '/home', redirect: '/welcome' },
      { path: '/welcome', component: Welcome },
      { path: '/users', component: Users },
      { path: '/rights', component: Rights },
      { path: '/roles', component: Roles },
      { path: '/categories', component: Cate },
      { path: '/params', component: Params },
      { path: '/goods', component: Goods },
      { path: '/goods', component: Goods },
      { path: '/goods/addGoods', component: AddGoods },
      { path: '/orders', component: Order },
      { path: '/reports', component: Report }
    ]
  }
]

const router = new VueRouter({
  routes
})
// 添加路由守卫beforeach，如果用户没有登录访问home页，则让他跳回登入页
router.beforeEach((to, from, next) => {
  // to: 表示要跳转那个网页
  // from: 表示从那个网页来
  // next: 表示放行
  // 其中 next() 表示放行  next('路径')表示强制跳转
  if (to.path === ('/login')) return next()
  // 获取token
  const loginToken = window.sessionStorage.getItem('token')
  // 通过判断token判断用户是否登录，登录则放行，没登录则强制跳转到登录页
  if (!loginToken) return next('/login')
  next()
})
export default router
